package net.xdclass.exception;


import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.utils.JsonData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
@ResponseBody
public class BizExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    private JsonData handler(Exception e) {
      if (e instanceof BizException){
          log.error("业务异常:{}",e);
          BizException bizException = (BizException) e;
          return JsonData.buildError(bizException.getCode(),bizException.getMsg());
      }else {
          log.error("非业务异常:{}",e);
          return JsonData.buildError("系统错误");
      }
    }
}
